using UnityEngine;
using UnityEngine.Events;

public class TimeCounter : MonoBehaviour
{
	private float excellentTime;

	private float maxTimeSeconds;

	private float currentSeconds;

	public UnityAction onTimeEnd;

	private float barWidth;

	private RectTransform barRT;

	private bool pause;

	private bool timerHidden;

	private Transform crownPin;

	private float translationOffsetY = -4f;

	private float animationShowTime = 0.6f;

	private float animationHideTime = 0.6f;

	private float animationCrown = 0.3f;

	private bool playedCrownAnimation;

	private float timeToCountdown = 4f;

	private void Awake()
	{
		DefineConnections();
	}

	private void DefineConnections()
	{
		barRT = base.transform.FindDeepChild("UI_Fill").GetComponent<RectTransform>();
		Vector2 sizeDelta = barRT.sizeDelta;
		barWidth = sizeDelta.x;
		crownPin = base.transform.FindDeepChild("UI_Crown_Pin");
	}

	public void InitTimer(float maxTimeSeconds, float excellentTime, UnityAction onTimeEnd)
	{
		this.onTimeEnd = onTimeEnd;
		if (excellentTime > maxTimeSeconds)
		{
			excellentTime = maxTimeSeconds;
		}
		this.maxTimeSeconds = maxTimeSeconds;
		currentSeconds = maxTimeSeconds;
		this.excellentTime = excellentTime;
		RectTransform rectTransform = barRT;
		float x = barWidth;
		Vector2 sizeDelta = barRT.sizeDelta;
		rectTransform.sizeDelta = new Vector3(x, sizeDelta.y);
		float num = (maxTimeSeconds - excellentTime) * barWidth / maxTimeSeconds - barWidth / 2f;
		Transform transform = crownPin.transform;
		float x2 = num;
		Vector3 localPosition = crownPin.transform.localPosition;
		float y = localPosition.y;
		Vector3 localPosition2 = crownPin.transform.localPosition;
		transform.localPosition = new Vector3(x2, y, localPosition2.z);
		crownPin.transform.localScale = new Vector3(1f, 1f, 1f);
		playedCrownAnimation = false;
		iTween.Stop(base.gameObject);
	}

	public float GetCurrentTime()
	{
		return currentSeconds;
	}

	public void SetCurrentTime(float time)
	{
		currentSeconds = time;
		if (currentSeconds > maxTimeSeconds)
		{
			maxTimeSeconds = currentSeconds;
		}
		UpdateBar();
	}

	public void PauseTimer()
	{
		pause = true;
		AudioController.instance.StopSounds("countdown");
	}

	public void ResumeTimer()
	{
		pause = false;
	}

	public void AddTime(float time)
	{
		currentSeconds += time;
		if (currentSeconds > maxTimeSeconds)
		{
			maxTimeSeconds = currentSeconds;
		}
		UpdateBar();
	}

	public void SubtractTime(float time)
	{
		currentSeconds -= time;
		if (currentSeconds <= 0f)
		{
			currentSeconds = 0f;
		}
		UpdateBar();
		if (currentSeconds <= 0f && onTimeEnd != null)
		{
			onTimeEnd();
		}
	}

	public void ShowTimer()
	{
		if (timerHidden)
		{
			timerHidden = false;
			iTween.MoveBy(base.gameObject, Vector3.down * translationOffsetY, animationShowTime);
		}
	}

	public void HideTimer(bool hideImmediately)
	{
		if (!timerHidden)
		{
			timerHidden = true;
			if (hideImmediately)
			{
				base.transform.Translate(Vector3.up * translationOffsetY);
			}
			else
			{
				iTween.MoveBy(base.gameObject, Vector3.up * translationOffsetY, animationHideTime);
			}
		}
	}

	private void Update()
	{
		if (currentSeconds > 0f && !pause)
		{
			currentSeconds = Mathf.Max(0f, currentSeconds - Time.deltaTime);
			if (timeToCountdown - currentSeconds >= 0f && timeToCountdown - (currentSeconds + Time.deltaTime) < 0f)
			{
				AudioController.instance.Play("countdown");
			}
			UpdateBar();
			UpdateCrown();
			if (currentSeconds <= 0f && onTimeEnd != null)
			{
				onTimeEnd();
			}
		}
	}

	private void UpdateBar()
	{
		if (currentSeconds <= 0f)
		{
			RectTransform rectTransform = barRT;
			Vector2 sizeDelta = barRT.sizeDelta;
			rectTransform.sizeDelta = new Vector3(0f, sizeDelta.y);
			return;
		}
		float num = barWidth * currentSeconds / maxTimeSeconds;
		float a = num;
		Vector2 sizeDelta2 = barRT.sizeDelta;
		num = Mathf.Lerp(a, sizeDelta2.x, 0.5f);
		RectTransform rectTransform2 = barRT;
		float x = num;
		Vector2 sizeDelta3 = barRT.sizeDelta;
		rectTransform2.sizeDelta = new Vector3(x, sizeDelta3.y);
	}

	private void UpdateCrown()
	{
		if (currentSeconds < maxTimeSeconds - excellentTime && !playedCrownAnimation)
		{
			playedCrownAnimation = true;
			GameObject gameObject = base.gameObject;
			object[] obj = new object[8]
			{
				"from",
				null,
				null,
				null,
				null,
				null,
				null,
				null
			};
			Vector3 localScale = base.transform.localScale;
			obj[1] = localScale.x;
			obj[2] = "to";
			obj[3] = 0f;
			obj[4] = "onupdate";
			obj[5] = "OnUpdateAnimation";
			obj[6] = "time";
			obj[7] = animationCrown;
			iTween.ValueTo(gameObject, iTween.Hash(obj));
		}
	}

	public void OnUpdateAnimation(float value)
	{
		crownPin.transform.localScale = new Vector3(value, value, value);
	}

	public bool IsCrownVisible()
	{
		if (playedCrownAnimation)
		{
			return false;
		}
		return true;
	}
}
